//
//  MovieLatticeViewController.m
//  豆瓣
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieLatticeViewController.h"
#import "MovieLatticeViewCell.h"
#import "MovieDetailViewController.h"
#import "AppTools.h"
#import "Movie.h"
@interface MovieLatticeViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)NSMutableArray *picArray;
@property (nonatomic, retain)UICollectionView *collectionView;

@end

@implementation MovieLatticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    flowLayout.itemSize = CGSizeMake(100, 160);
    
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    flowLayout.minimumLineSpacing = 30;
    
    flowLayout.minimumInteritemSpacing = 10;
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    [self.view addSubview:self.collectionView];
    [self.collectionView release];
    
    self.picArray = [NSMutableArray array];
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";

    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerClass:[MovieLatticeViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [AppTools getDataWithURL:urlStr block:^(id result) {
        
        NSMutableDictionary *dic = result;
        
        NSString *reason = [dic objectForKey:@"reason"];
        
        NSMutableArray *array = [dic objectForKey:@"result"];
        
        if ([reason isEqualToString:@"success"]) {
            
            for (NSDictionary *dic in array) {
                
                Movie *movie = [[Movie alloc] init];
                
                [movie setValuesForKeysWithDictionary:dic];
                
                [self.picArray addObject:movie];
                
                [movie release];
            }
            
            [self.collectionView reloadData];
            
        }else{
            
            NSLog(@"请求失败");
            
        }
        
    }];

    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.picArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"cell";
    MovieLatticeViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    Movie *movie = [self.picArray objectAtIndex:indexPath.row];
    
    cell.movieL = movie;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    MovieDetailViewController *movieDVC = [[MovieDetailViewController alloc] init];
    
    Movie *movie = [self.picArray objectAtIndex:indexPath.row];
    
    movieDVC.movieID = movie.movieId;
    
    [self.navigationController pushViewController:movieDVC animated:YES];
    
    [movieDVC release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
